package com.tian.photopicker;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.tian.photopicker.ui.PhotoPickerActivity;
import com.tian.photopicker.util.PermissionsUtils;

/**
 * 选择构建类
 * @author tian
 */

public class PhotoPicker {
    public static final String SELECT_PHOTO_COUNT = "select_photo_count";
    public static final String IS_SHOW_CAMERA = "is_show_camera";
    public static final String IS_USE_HEAD = "is_use_head";
    public static final String IS_CROP = "is_crop";
    public static final String RECYCLER_VIEW_COLUMNS = "recycler_view_columns";
    public static final String KEY_SELECTED_PHOTOS = "select_key";
    public static PickerBuilder builder() {
        return new PickerBuilder();
    }
    /**
        builder模式
     */
    public static class PickerBuilder {
        /**
         * 数据传递
         */
        private Bundle dataBundle;
        /**
         * 意图
         */
        private Intent intent;

        /**
         * 构造方法
         */
        public PickerBuilder() {
            dataBundle = new Bundle();
            intent = new Intent();
        }

        /**
         * 选择图片数量
         * @param count 数量值 默认是9
         * @return
         */
        public PickerBuilder setSelectPhotoCount(int count) {
            dataBundle.putInt(SELECT_PHOTO_COUNT,count);
            return this;
        }
        /**
         * 是否显示照相机 默认是true
         * @param showCamera
         * @return
         */
        public PickerBuilder setShowCamera(boolean showCamera) {
            dataBundle.putBoolean(IS_SHOW_CAMERA,showCamera);
            return this;
        }
        /**
         * 是否用于头像
         * true   只能选取一张 传入的setSelectPhotoCount 失效
         * @param use
         * @return
         */
        public PickerBuilder useHead(boolean use) {
            dataBundle.putBoolean(IS_USE_HEAD,use);
            return this;
        }
        /**
         * 是否用裁剪
         * true  只能选取一张 传入的setSelectPhotoCount 失效
         * @param isCrop
         * @return
         */
        public PickerBuilder crop(boolean isCrop) {
            dataBundle.putBoolean(IS_CROP,isCrop);
            return this;
        }
        /**
         * 设置列表列数 默认是3
         * @param columns
         * @return
         */
        public PickerBuilder setColumns(int columns) {
            dataBundle.putInt(RECYCLER_VIEW_COLUMNS,columns);
            return this;
        }

        /**
         * 从activity启动
         * @param activity
         * @param requestCode
         */
        public void start (Activity activity,int requestCode) {
            if (PermissionsUtils.checkReadStoragePermission(activity)) {
                activity.startActivityForResult(getIntent(activity), requestCode);
            }
        }

        /**
         * 从fragment启动
         * @param fragment
         * @param requestCode
         */
        public void start (Fragment fragment, int requestCode) {
            if (PermissionsUtils.checkReadStoragePermission(fragment.getActivity())) {
                fragment.startActivityForResult(getIntent(fragment.getActivity()), requestCode);
            }
        }
        private Intent getIntent(Context context) {
            intent.setClass(context,PhotoPickerActivity.class);
            intent.putExtras(dataBundle);
            return intent;
        }
    }
}
